﻿//=======================================================
// Author: Jonas Follesø <jonas@follesoe.no>
// Copyright (c) 2008
//
// Licensed under the Microsoft Public License (Ms-PL)
// A copy of the license is available at 
// http://www.opensource.org/licenses/ms-pl.html
//=======================================================

using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using Colorful.Control.Properties;

namespace Colorful.Control.Services
{
    public class AdobeClipboardFormatter : ICopyColors
    {
        public string FormatName
        {
            get { return "ADOBE AI3"; }
        }

        public void AddToClipboard(DataObject dataObject, Palette palette)
        {
            string illustratorClipboard = Resources.AdobeIllustratorClipboardTemplate;
            illustratorClipboard = illustratorClipboard.Replace("{color1}", NormalizeColor(palette.Color1));
            illustratorClipboard = illustratorClipboard.Replace("{color2}", NormalizeColor(palette.Color2));
            illustratorClipboard = illustratorClipboard.Replace("{color3}", NormalizeColor(palette.Color3));
            illustratorClipboard = illustratorClipboard.Replace("{color4}", NormalizeColor(palette.Color4));
            illustratorClipboard = illustratorClipboard.Replace("{color5}", NormalizeColor(palette.Color5));
            illustratorClipboard = illustratorClipboard.Replace("{colorName}", palette.Name);

            MemoryStream ms = new MemoryStream();
            using(StreamWriter sw = new StreamWriter(ms))
            {                
                sw.Write(illustratorClipboard);
                sw.Flush();
                ms.Seek(0, SeekOrigin.Begin);
                dataObject.SetData(FormatName, new MemoryStream(ms.GetBuffer()), true);
            }
        }

        internal string NormalizeColor(Color color)
        {
            float nR = color.R / 255.0f;
            float nG = color.G / 255.0f;
            float nB = color.B / 255.0f;
            return string.Format("{0} {1} {2}", nR, nG, nB);
        }
    }
}